﻿using Mogubang_Sl.Entity;
using System;
using System.Collections.Generic;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Text;

namespace Mogubang_Sl.Core
{
    public class StageManager
    {
        public static StageManager Instance = new StageManager();

        //keys
        private const string LAST_STAGE_DATA_KEY = "LastStageDataKey";
        private const string IS_PLAY_BACKGROUND_KEY = "isPlayBackgroundKey";
        private const string IS_PLAY_SOUND_KEY = "isPlaySoundKey";
        private const string TOTALGKEY = "TotalGKey";
        private const string SAVED_GAME_RECORD = "savedGameRecordKey";
        private const string GAME_HIGHLIGHTS_KEY = "gameHighlightsKey";
        private const string WOOD_COUNT_KEY = "WoodCOuntKey";
        private const string BOMB_COUNT_KEY = "BombCOuntKey";
        private const string HIGH_SCORE_KEY = "HIGH_SCOREkey";

        private const string LAUNCH_COUNT = "launchCount";
        private const string MARKET_COMMENT = "marketComment";
        private const string NEW_USER_TASK_KEY = "newUserTaskKey";
        private const string ISFIRSTGAME_KEY = "ISFIRSTGAME_KEY";


        private StageManager()
        {

        }

        private int highScore = 0;
        public int HighScore
        {
            get
            {
                return highScore;
            }
            set
            {
                highScore = value;
                IsolatedStorageSettings.ApplicationSettings[HIGH_SCORE_KEY] = highScore;
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
        }

        private bool isFirstGame = true;
        public bool IsFirstGame
        {
            get
            {
                return isFirstGame;
            }
            set
            {
                isFirstGame = value;
                IsolatedStorageSettings.ApplicationSettings[ISFIRSTGAME_KEY] = isFirstGame;
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
        }

        private int launchCount = 0;
        public int LaunchCount
        {
            get
            {
                return launchCount;
            }
            set
            {
                launchCount = value;
                IsolatedStorageSettings.ApplicationSettings[LAUNCH_COUNT] = launchCount;
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
        }

        private bool isDoNewUserTask = false;
        public bool IsDoNewUserTask
        {
            get
            {
                return isDoNewUserTask;
            }
            set
            {
                isDoNewUserTask = value;
                IsolatedStorageSettings.ApplicationSettings[NEW_USER_TASK_KEY] = isDoNewUserTask;
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
        }

        private bool isCommented = false;
        public bool IsCommented
        {
            get
            {
                return isCommented;
            }
            set
            {
                isCommented = value;
                IsolatedStorageSettings.ApplicationSettings[MARKET_COMMENT] = isCommented;
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
        }

        private bool isPlaySound = true;

        /// <summary>
        /// 是否播放音效
        /// </summary>
        public bool IsPlaySound
        {
            get
            {
                return isPlaySound;
            }
            set
            {
                isPlaySound = value;
                IsolatedStorageSettings.ApplicationSettings[IS_PLAY_SOUND_KEY] = isPlaySound;
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
        }

        private bool isPlayBackground = true;
        /// <summary>
        /// 是否播放背景音乐
        /// </summary>
        public bool IsPlayBackground
        {
            get
            {
                return isPlayBackground;
            }
            set
            {
                isPlayBackground = value;
                IsolatedStorageSettings.ApplicationSettings[IS_PLAY_BACKGROUND_KEY] = isPlayBackground;
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
        }

        private LastStageInfo lastStage;
        /// <summary>
        /// 上一个关卡
        /// </summary>
        public LastStageInfo LastStage
        {
            get
            {
                return lastStage;
            }
            set
            {
                lastStage = value;
                IsolatedStorageSettings.ApplicationSettings[LAST_STAGE_DATA_KEY] = lastStage;
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
        }

        private int totalG;

        /// <summary>
        /// 金币
        /// </summary>
        public int TotalG
        {
            get
            {
                return totalG;
            }
            set
            {
                totalG = value;
                IsolatedStorageSettings.ApplicationSettings[TOTALGKEY] = TotalG;
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
        }

        private int woodCount = 5;

        /// <summary>
        /// 金币
        /// </summary>
        public int WoodCount
        {
            get
            {
                return woodCount;
            }
            set
            {
                woodCount = value;
                IsolatedStorageSettings.ApplicationSettings[WOOD_COUNT_KEY] = woodCount;
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
        }

        private int bombCount = 10;

        /// <summary>
        /// 金币
        /// </summary>
        public int BombCount
        {
            get
            {
                return bombCount;
            }
            set
            {
                bombCount = value;
                IsolatedStorageSettings.ApplicationSettings[BOMB_COUNT_KEY] = bombCount;
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
        }

        public void Init()
        {
            //TODO 初始化
            if (IsolatedStorageSettings.ApplicationSettings.Contains(LAUNCH_COUNT))
            {
                launchCount = (int)IsolatedStorageSettings.ApplicationSettings[LAUNCH_COUNT];
            }

            if (IsolatedStorageSettings.ApplicationSettings.Contains(MARKET_COMMENT))
            {
                isCommented = (bool)IsolatedStorageSettings.ApplicationSettings[MARKET_COMMENT];
            }

            if (IsolatedStorageSettings.ApplicationSettings.Contains(NEW_USER_TASK_KEY))
            {
                isDoNewUserTask = (bool)IsolatedStorageSettings.ApplicationSettings[NEW_USER_TASK_KEY];
            }

            if (IsolatedStorageSettings.ApplicationSettings.Contains(LAST_STAGE_DATA_KEY) && IsolatedStorageSettings.ApplicationSettings[LAST_STAGE_DATA_KEY] != null)
            {
                lastStage = (LastStageInfo)IsolatedStorageSettings.ApplicationSettings[LAST_STAGE_DATA_KEY];
            }

            if (IsolatedStorageSettings.ApplicationSettings.Contains(IS_PLAY_SOUND_KEY) && IsolatedStorageSettings.ApplicationSettings[IS_PLAY_SOUND_KEY] != null)
            {
                isPlaySound = (bool)IsolatedStorageSettings.ApplicationSettings[IS_PLAY_SOUND_KEY];
            }

            if (IsolatedStorageSettings.ApplicationSettings.Contains(IS_PLAY_BACKGROUND_KEY))
            {
                isPlayBackground = (bool)IsolatedStorageSettings.ApplicationSettings[IS_PLAY_BACKGROUND_KEY];
            }

            if (IsolatedStorageSettings.ApplicationSettings.Contains(ISFIRSTGAME_KEY))
            {
                isFirstGame = (bool)IsolatedStorageSettings.ApplicationSettings[ISFIRSTGAME_KEY];
            }

            if (IsolatedStorageSettings.ApplicationSettings.Contains(TOTALGKEY) && IsolatedStorageSettings.ApplicationSettings[TOTALGKEY] != null)
            {
                totalG = (int)IsolatedStorageSettings.ApplicationSettings[TOTALGKEY];
                //TODO lc 测试使用,发布时去除
                //totalG += 10000000;
            }
            else
            {
                //初始金币
                totalG = GameStatics.InitTotalG;
            }

            if (IsolatedStorageSettings.ApplicationSettings.Contains(BOMB_COUNT_KEY))
            {
                bombCount = (int)IsolatedStorageSettings.ApplicationSettings[BOMB_COUNT_KEY];
            }
            if (IsolatedStorageSettings.ApplicationSettings.Contains(WOOD_COUNT_KEY))
            {
                woodCount = (int)IsolatedStorageSettings.ApplicationSettings[WOOD_COUNT_KEY];
            }
            //test
            //woodCount = 99;
            if (IsolatedStorageSettings.ApplicationSettings.Contains(HIGH_SCORE_KEY))
            {
                highScore = (int)IsolatedStorageSettings.ApplicationSettings[HIGH_SCORE_KEY];
            }
        }


    }
}
